###########################################################################
# 交通灯
# 作者：张立业
# Copyright: 山东科技大学，交通学院。 All right reserved.
#
##########################################################################

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class TrafficLight:
    def __init__(self, scene, x, y):
        '''

        scene - 场景对象
        pos - x, y, 交通灯的位置
        '''
        self.mState = -1

        segment = QGraphicsRectItem(x, y, 20, 45)  # 路段宽度1000
        segment.setBrush(QBrush(Qt.gray))
        scene.addItem(segment)

        self.mRedLight = QGraphicsEllipseItem( x+5, y+10, 8, 8)  # 路段宽度1000
        self.mRedLight.setBrush( QBrush(Qt.gray) )
        scene.addItem( self.mRedLight )

        self.mYellowLight = QGraphicsEllipseItem( x+5, y+20, 8, 8)  # 路段宽度1000
        self.mYellowLight.setBrush( QBrush(Qt.gray) )
        scene.addItem( self.mYellowLight )

        self.mGreenLight = QGraphicsEllipseItem( x+5, y+30, 8, 8)  # 路段宽度1000
        self.mGreenLight.setBrush( QBrush(Qt.gray) )
        scene.addItem( self.mGreenLight )

    def setState(self, st):
        ''' 设置交通灯的状态

        st - 交通状态，integer, 0 - 红，1-黄， 2 - 绿， -1 没亮灯
        '''
        self.mState = st
        if st==0:  # red
            self.mRedLight.setBrush(QBrush(Qt.red))
            self.mYellowLight.setBrush(QBrush(Qt.gray))
            self.mGreenLight.setBrush( QBrush(Qt.gray) )
        elif st==1:  # yello
            self.mRedLight.setBrush(QBrush(Qt.gray))
            self.mYellowLight.setBrush(QBrush(Qt.yellow))
            self.mGreenLight.setBrush( QBrush(Qt.gray) )
        elif st==2:  # green
            self.mRedLight.setBrush(QBrush(Qt.gray))
            self.mYellowLight.setBrush(QBrush(Qt.gray))
            self.mGreenLight.setBrush( QBrush(Qt.green) )
        else:  # red
            self.mRedLight.setBrush(QBrush(Qt.gray))
            self.mYellowLight.setBrush(QBrush(Qt.gray))
            self.mGreenLight.setBrush( QBrush(Qt.gray) )

    def getState(self):
        ''' 获得交通灯的状态

        return - 交通状态，integer, 0 - 红，1-黄， 2 - 绿， -1 没亮灯
        '''
        return self.mState

    def setPos(self, pos):
        """

        pos - (x, y) 交通灯的坐标
        """
        pass

